<?php

/**
 * FootballFactory, Copyright Robin Loop & Pascal Wirz, 2014
 * ---
 * Stellt alle Funktionen zum Auflisten und Editieren der Spiele zur Verfügung
 *
 * @author Robin
 */
include "redirector.php";
include "../ff_includes/class_database.php";
include "../ff_includes/class_game.php";
include "../ff_includes/class_season.php";
include "../ff_includes/class_team.php";
include "../ff_includes/class_gamevenue.php";
include "../ff_includes/class_gametype.php";
include "../ff_includes/class_player.php";
include "../ff_includes/class_scorer.php";

$action = isset($_GET['action']) ? $_GET['action'] : "";

switch ($action) {
    case 'list':
        listGames();
        break;
    case 'new':
        newGame();
        break;
    case 'edit':
        editGame();
        break;
    case 'save':
        saveGame();
        break;
    case 'delete':
        deleteGame();
        break;
    default:
        listGame();
}

function listGames() {
    $db = new Database();
    $teamId = isset($_GET['teamId']) ? (int) $_GET['teamId'] : -1;
    $seasonId = isset($_GET['seasonId']) ? (int) $_GET['seasonId'] : -1;

    // Spielliste anhand der gesetzten Filter-Parameter holen
    if ($teamId > 0) {
        if ($seasonId > 0) {
            $gameList = Game::getGamesByTeamAndSeason($teamId, $seasonId);
        } else {
            $gameList = Game::getGamesByTeam($teamId);
        }
    } else {
        if ($seasonId > 0) {
            $gameList = Game::getGamesBySeason($seasonId);
        } else {
            $gameList = Game::getGames();
        }
    }
    $numbGames = $gameList['totalRows'];
    $games = $gameList['gameList'];

    // Liste der Saisons und Teams fuer die Filterauswahl
    $seasonList = Season::getSeasonList();
    $seasons = $seasonList['seasonList'];
    $teamList = Team::getTeamList();
    $teams = $teamList['teamList'];

    require("templates/game_list.php");
}

function newGame() {
    $db = new Database();
    $game = new Game();
    $gamevenueList = Gamevenue::getGamevenueList();
    $gamevenues = $gamevenueList['gamevenueList'];
    $gametypeList = Gametype::getGametypeList();
    $gametypes = $gametypeList['gametypeList'];
    $seasonList = Season::getSeasonList();
    $seasons = $seasonList['seasonList'];
    $teamList = Team::getTeamList();
    $teams = $teamList['teamList'];
    $playerList = Player::getPlayers();
    $players = $playerList['playerList'];
    $scorers = [];

    require("templates/game_edit.php");
}

function editGame() {
    $db = new Database();
    $gameId = (int) $_GET['gameId'];
    $game = Game::getGameById($gameId);
    //$playerSeasonList = Player::getSeasonsByPlayerId($playerid);
    // Liste der Saisons, Teams, Spielorte, Spieler und Spieltypen fuer die Zuweisung
    $seasonList = Season::getSeasonList();
    $seasons = $seasonList['seasonList'];
    $teamList = Team::getTeamList();
    $teams = $teamList['teamList'];
    $gamevenueList = Gamevenue::getGamevenueList();
    $gamevenues = $gamevenueList['gamevenueList'];
    $gametypeList = Gametype::getGametypeList();
    $gametypes = $gametypeList['gametypeList'];
    $playerList = Player::getPlayers();
    $players = $playerList['playerList'];
    
    $scorersList = Scorer::getScorersByGameId($gameId);
    $scorers = $scorersList['scorerList'];
    
    require("templates/game_edit.php");
}

function saveGame() {
    // speichern
    if (isset($_POST['saveChanges'])) {
        $gameArray["game_id"] = $_POST["game_id"];
        $gameArray["game_hometeam"] = $_POST["game_hometeam"];
        $gameArray["game_awayteam"] = $_POST["game_awayteam"];
        $gameArray["game_date"] = $_POST["game_date"];
        $gameArray["game_type"] = $_POST["game_type"];
        $gameArray["game_imagelink"] = $_POST["game_imagelink"];
        $gameArray["game_reportlink"] = $_POST["game_reportlink"];
        $gameArray["game_goalshome"] = $_POST["game_goalshome"];
        $gameArray["game_goalsaway"] = $_POST["game_goalsaway"];
        $gameArray["game_team"] = $_POST["game_team"];
        $gameArray["game_season"] = $_POST["game_season"];
        $gameArray["game_venue"] = $_POST["game_venue"];
        
        $game = new Game($gameArray);
        
        // update
        if ($game->getId() != 0) {
            $game->update();
            
            $gametype = Gametype::getGameTypeByGameId($game->getId());
            if ($gametype->getHome() == true) {
                $anzahlScorers = $game->getGoalshome();
            } else {
                $anzahlScorers = $game->getGoalsaway();
            }
            // scorers loeschen
            $emptyScorerList;
            for ($i = 0; $i<$anzahlScorers; $i++) {
                $emptyScorerList[$i] = -1;
            }
            $game->setScorers($emptyScorerList);
            
            if (isset($_POST['game_scorers'])) {
                $scorerIds = $_POST['game_scorers'];
                $game->setScorers($scorerIds);
            }
        }
        // neuer Eintrag
        else {
            $game->insert();
            
            $gametype = Gametype::getGameTypeByGameId($game->getId());
            if ($gametype->getHome() == true) {
                $anzahlScorers = $game->getGoalshome();
            } else {
                $anzahlScorers = $game->getGoalsaway();
            }
            // scorers loeschen
            $emptyScorerList;
            for ($i = 0; $i<$anzahlScorers; $i++) {
                $emptyScorerList[$i] = -1;
            }
            $game->setScorers($emptyScorerList);
            
            if (isset($_POST['game_scorers'])) {
                $scorerIds = $_POST['game_scorers'];
                $game->setScorers($scorerIds);
            }
        }
        header("Location: game.php?action=list");
    }
    // abbrechen
    elseif (isset($_POST['cancel'])) {
        header("Location: game.php?action=list");
    }
}

function deleteGame() {
    $game = Game::getGameById((int) $_GET['gameId']);
    //$player->setSeasons(array()); // leerer Array um den Spieler aus der Verknuepfungstabelle zu loeschen
    $game->delete();
    header("Location: game.php?action=list");
}

?>
